//
//  RecentChatCellView.m
//  JiaoX
//
//  Created by yinqy on 15/3/18.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "RecentChatCellView.h"




#define NAME_LABEL_FONT [UIFont systemFontOfSize:[DimensUtils getScaleX:14]]
#define SUB_LABEL_FONT [UIFont systemFontOfSize:[DimensUtils getScaleX:12]]
#define RED_POINT_WIDTH [DimensUtils getScaleX:16]
@interface RecentChatCellView()
{
    UILabel *_redPoint;
    UIImageView *_headImg;
    UILabel *_nameLabel;
    UILabel *_subLabel;
    UIButton *_timeBtn;
}

@end
@implementation RecentChatCellView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //头像
        _headImg = [[UIImageView alloc]initWithFrame:CGRectMake(10, 15, IMAGE_HEIGHT, IMAGE_HEIGHT)];
        [_headImg.layer setCornerRadius:_headImg.height/2];
        [_headImg.layer setMasksToBounds:YES];
        [self addSubview:_headImg];
        
        //红点
        _redPoint = [[UILabel alloc]init];
        [_redPoint setBackgroundColor:[UIColor redColor]];
        [_redPoint setTextColor:[UIColor whiteColor]];
        [_redPoint setTextAlignment:NSTextAlignmentCenter];
        [_redPoint setFont:[UIFont systemFontOfSize:[DimensUtils getScaleX:10]]];
        [_redPoint setFrame:CGRectMake(CGRectGetMaxX(_headImg.frame)-RED_POINT_WIDTH*0.5-5, _headImg.y-5, RED_POINT_WIDTH, RED_POINT_WIDTH)];
        [_redPoint.layer setCornerRadius:RED_POINT_WIDTH/2];
        [_redPoint.layer setMasksToBounds:YES];
        [self addSubview:_redPoint];
        
        //名称
        _nameLabel = [[UILabel alloc]init];
        [_nameLabel setTextColor:JXColor(51, 51, 51)];
        [_nameLabel setFont:NAME_LABEL_FONT];
        [_nameLabel setX:CGRectGetMaxX(_headImg.frame)+5];
        [_nameLabel setY:CGRectGetMinY(_headImg.frame)+2];
        [self addSubview:_nameLabel];
        
        //子标题
        _subLabel = [[UILabel alloc]init];
        [_subLabel setTextColor:JXColor(102, 102, 102)];
        [_subLabel setFont:SUB_LABEL_FONT];
        [_subLabel setX:_nameLabel.x];
        [_subLabel setY:CGRectGetMaxY(_headImg.frame)-SUB_LABEL_FONT.lineHeight-2];
        [_subLabel setSize:CGSizeMake(JXScreenW-_subLabel.x-10, _subLabel.font.lineHeight)];
        [self addSubview:_subLabel];
        
        _timeBtn = [[UIButton alloc]init];
        [_timeBtn setUserInteractionEnabled:NO];
        [_timeBtn setImage:[UIImage imageNamed:@"ic_time_tint"] forState:UIControlStateNormal];
        [_timeBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
        [_timeBtn setTitleColor:JXColor(153, 153, 153) forState:UIControlStateNormal];
        [_timeBtn.titleLabel setFont:[UIFont systemFontOfSize:[DimensUtils getScaleX:10]]];
        [_timeBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
        [self addSubview:_timeBtn];
    }
    return self;
}

-(void)setConverstaion:(Conversation *)converstaion{
    _converstaion = converstaion;

    [_headImg sd_setImageWithURL:[NSURL URLWithString:converstaion.toUser.userImg]];
    
    [_nameLabel setText:converstaion.toUser.nickName];
    [_nameLabel setSize:[_nameLabel.text sizeWithFont:_nameLabel.font]];
    if (converstaion.msg.msgType==MsgTypeText) {
        [_subLabel setText:[converstaion.msg actualContent]];
    }else{
        [_subLabel setText:@"[图片]"];
    }
    
    if (converstaion.unreadCount) {
        [_redPoint setHidden:NO];
        [_redPoint setText:[NSString stringWithFormat:@"%d",converstaion.unreadCount]];
    }else{
        [_redPoint setHidden:YES];
    }
    [_timeBtn setTitle:[Utils millisecs2DateString:converstaion.msg.timestamp] forState:UIControlStateNormal];
    CGSize size = [_timeBtn.titleLabel.text sizeWithFont:_timeBtn.titleLabel.font];
    CGFloat timeW =size.width+5+_timeBtn.imageView.image.size.width;
    CGFloat timeH = size.height;
    [_timeBtn setSize:CGSizeMake(timeW, timeH)];
    _timeBtn.centerY = _nameLabel.centerY;
    _timeBtn.x = JXScreenW-10-_timeBtn.width;
}

@end
